package com.lvy.hczn.front.business.controller;

import java.util.List;

import com.lvy.hczn.front.business.service.WarehousingOutService;
import com.lvy.hczn.front.business.util.ResultUtil;
import com.lvy.hczn.front.common.exception.UtilException;
import com.lvy.hczn.front.system.domain.business.WarehousingOut;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.lvy.hczn.front.common.annotation.Log;
import com.lvy.hczn.front.common.enums.BusinessType;
import com.lvy.hczn.front.common.core.controller.BaseController;
import com.lvy.hczn.front.common.core.domain.AjaxResult;
import com.lvy.hczn.front.common.utils.poi.ExcelUtil;
import com.lvy.hczn.front.common.core.page.TableDataInfo;

/**
 * 商品出库单Controller
 *
 * @author ruoyi
 * @date 2023-05-30
 */
@Controller
@RequestMapping("/api/dt/system/out")
@Slf4j
public class WarehousingOutController extends BaseController {
    private String prefix = "system/out";

    @Autowired
    private WarehousingOutService warehousingOutService;

    @RequiresPermissions("system:out:view")
    @GetMapping()
    public String out() {
        return prefix + "/out";
    }

    /**
     * 查询商品出库单列表
     */
    @RequiresPermissions("system:out:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(WarehousingOut warehousingOut) {
        startPage();
        List<WarehousingOut> list = warehousingOutService.selectWarehousingOutList(warehousingOut);
        return getDataTable(list);
    }

    /**
     * 导出商品出库单列表
     */
    @RequiresPermissions("system:out:export")
    @Log(title = "商品出库单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(WarehousingOut warehousingOut) {
        List<WarehousingOut> list = warehousingOutService.selectWarehousingOutList(warehousingOut);
        ExcelUtil<WarehousingOut> util = new ExcelUtil<WarehousingOut>(WarehousingOut.class);
        return util.exportExcel(list, "商品出库单数据");
    }

    /**
     * 新增商品出库单
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存商品出库单
     */
    @RequiresPermissions("system:out:add")
    @Log(title = "商品出库单", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(WarehousingOut warehousingOut) {
        return toAjax(warehousingOutService.insertWarehousingOut(warehousingOut));
    }

    /**
     * 修改商品出库单
     */
    @RequiresPermissions("system:out:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap) {
        WarehousingOut warehousingOut = warehousingOutService.selectWarehousingOutById(id);
        mmap.put("warehousingOut", warehousingOut);
        return prefix + "/edit";
    }

    /**
     * 修改保存商品出库单
     */
    @RequiresPermissions("system:out:edit")
    @Log(title = "商品出库单", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(WarehousingOut warehousingOut) {
        return toAjax(warehousingOutService.updateWarehousingOut(warehousingOut));
    }

    /**
     * 删除商品出库单
     */
    @RequiresPermissions("system:out:remove")
    @Log(title = "商品出库单", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(warehousingOutService.deleteWarehousingOutByIds(ids));
    }

    @ApiOperation(value = "提交出库单到平台", notes = "提交出库单到平台")
    @PostMapping("/commitToPlatform")
    @ResponseBody
    public ResultUtil commitToPlatform() {
        try {
            WarehousingOut warehousingOut = new WarehousingOut();
            warehousingOut.setBatchCode("1571693679366742016");
            warehousingOut.setBatchCode("1571693679366742016");
            warehousingOutService.commitToPlatform(warehousingOut);
            return ResultUtil.ok();
        } catch (Exception e) {
            log.error("commitToPlatform:{}", e);
            if (e instanceof UtilException) {
                return ResultUtil.failed(e.getMessage());
            } else {
                e.printStackTrace();
                return ResultUtil.failed("系统异常");
            }
        }
    }
}
